package org.example.algorithm.binarytree;

import org.example.algorithm.TreeNode;

public class DiameterSolution {
    private int diameter;
    public static void main(String[] args) {

    }

    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return diameter;
    }

    public int maxDepth(TreeNode node) {
        if (node == null) {
            return -1;
        }
        int leftDepth = maxDepth(node.left);
        int rightDepth = maxDepth(node.right);
        diameter = Math.max(diameter, leftDepth + rightDepth + 2);
        return Math.max(leftDepth, rightDepth) + 1;
    }


}
